/*
 Creator: Kisamin Resident and copyright by her.  All rights reserved except those specifically set forth here:
 You may:
 	 Use these scripts in any project
	 Modify these scripts in any way you wish subject to:
	 		1. Retaining this set of comments
			2. Explicity commenting and showing any changes you might make
 You may NOT:
 	 License these scripts to someone else with restrictions differing from the above.
	 Charge a fee for the scripts themselves (although you may charge for an object that
	 		utilizes these scripts to add functionality)
	 Charge a fee for any "library" or "collection" of scripts that includes this script.

If you have questions about your rights under this coment and/or the Gnu CopyLeft, please request further information
and/or explicit permission from Kisamin Resident
*/

string g_sMunitionName;

vector g_vOffset = <5.0, 0., 0.>;
vector g_vInitialVelocity = <200.0, 0., 0.>;





doLaunch () {
    llOwnerSay ("doLaunch");
    if (g_sMunitionName == "") {
        llOwnerSay ("No munition to launch.");
        return;
    }
    
    rotation l_rDirection;
    if (llGetAttached () == 0)
        l_rDirection = llGetRootRotation ();
    else
        l_rDirection = llGetCameraRot ();
    
    llRezObject (g_sMunitionName, llGetRootPosition () + (g_vOffset * l_rDirection), g_vInitialVelocity, l_rDirection, 1);
}




getMunitionName () {
    integer l_iNumObjects = llGetInventoryNumber (INVENTORY_OBJECT);
    
    integer i;
	for (i=0; i<=l_iNumObjects; i++) {
        string l_sObjectName = llGetInventoryName (INVENTORY_OBJECT, i);
        if (llSubStringIndex (g_sMunitionName, l_sObjectName) == 0 || g_sMunitionName == "")
            g_sMunitionName = l_sObjectName;
    }
    
    integer l_iMaxRevisionNumber = 0;
    for (i=0; i<l_iNumObjects; i++) {
        string l_sObjectName = llGetInventoryName (INVENTORY_OBJECT, i);
        if (g_sMunitionName == "")
            g_sMunitionName = l_sObjectName;
        else if (llSubStringIndex (g_sMunitionName, l_sObjectName) == 0)
            g_sMunitionName = l_sObjectName;
        else {
            list l_lParts = llParseString2List (l_sObjectName, [" "], []);
            string l_sNumber = llList2String (l_lParts, -1);
            integer l_iNumber = (integer)l_sNumber;
            if (l_iNumber > 0 && l_iNumber > l_iMaxRevisionNumber)
                l_iMaxRevisionNumber = l_iNumber;
        }
    }
    
    if (l_iMaxRevisionNumber)
        g_sMunitionName += " " + (string)l_iMaxRevisionNumber;
    
    if (g_sMunitionName == "")
        llOwnerSay ("No munition available.");
    else
        llOwnerSay ("Using munition: " + g_sMunitionName);
}





default {
    state_entry () {
        getMunitionName ();
    }
    
    changed (integer in_iChange) {
        if (in_iChange & CHANGED_INVENTORY)
            getMunitionName ();
    }
    
    attach (key in_kAvatar) {
        llRequestPermissions (llGetOwner (), PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA);
    }
    
    on_rez (integer in_iParam) {
        if (llGetAttached () != 0)
            llRequestPermissions (llGetOwner (), PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA);
    }
    
    run_time_permissions (integer in_iPermissions) {
        if (in_iPermissions & PERMISSION_TAKE_CONTROLS)
            llTakeControls (CONTROL_ML_LBUTTON, TRUE, TRUE);
        else
            llOwnerSay ("Weapon is inoperable without permissions.  Weapon is disabled until you remove and wear it again.");
    }
    
    touch_start (integer in_iNumTouchers) {
        llOwnerSay ("Touched!");
        if (llGetAttached () == 0)
            doLaunch ();
    }
    
    control (key in_kAvatar, integer in_iHeld, integer in_iEdge) {
        if (llGetAttached () != 0)
            doLaunch ();
    }
}
